package com.suxii.netty_demo.activity;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.suxii.netty_demo.R;
import com.suxii.netty_demo.fragment.FriendFragment;
import com.suxii.netty_demo.fragment.MessageFragment;
import com.suxii.service.MyNettyService;
import com.suxii.test.Main;
import com.suxii.utils.Global;
import com.suxii.widget.CircleOpenView;
import com.suxii.widget.NoBar;
import com.suxii.widget.SlideMenuView;
import com.suxii.widget.dialog.CustomDialog;

import java.io.File;
import java.util.Arrays;

public class Suxii extends AppCompatActivity {

    private MessageFragment messageFragment;
    private FriendFragment friendFragment;

    private Context context;

    // 选择控件
    public static CircleOpenView circleOpenView;

    public TextView connectText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NoBar.setNoBar(this);
        setContentView(R.layout.activity_suxii);
        context = this;

        connectText = findViewById(R.id.connect_test);
        circleOpenView = findViewById(R.id.suxii_circle);

        // 初始化顶部文字
        setConnectText();
        // 初始化滑动控件选择监听
        setCircleLinener();

        // 初始化fragment
        init();

        // 弹窗初始化
        dialog();

        //侧滑界面绑定
        leftLinener();
    }

    private SlideMenuView leftView;

    public static CardView circle=null;
    private void leftLinener() {
        circle=findViewById(R.id.suxii_left_circle);
        TextView leftName = findViewById(R.id.suxii_left_name);
        leftView = findViewById(R.id.suxii_leftView);
        leftName.setText(Global.name);
        findViewById(R.id.suxii_left_btn_cancel).setOnClickListener(v->{
            mDialog.setCanotBackPress();
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.show();
        });
    }

    private void deleteUid(){
        File file = new File(String.valueOf(getFilesDir()));
        File[] files = file.listFiles();
        Arrays.stream(files).forEach(f->f.delete());

        Global.setUid(0);
        Intent service = new Intent(this, MyNettyService.class);
        stopService(service);

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();

    }

    private void dialog() {
        mDialog = new CustomDialog(this, "温馨提示", "确定将注销当前账户，并清理全部本地聊天记录", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                deleteUid();
            }
        }, "取消");
    }

    private CustomDialog mDialog;

    public void addFriend(View view) {
        startActivity(new Intent(context,AddFriendActivity.class));

    }


    private ValueAnimator valueAnimator;

    private float yValue;
    private void testAnim(boolean isStart) {
        if (isStart) {
            yValue=-250;
            valueAnimator = ValueAnimator.ofFloat(yValue, 0);
        } else {
            valueAnimator = ValueAnimator.ofFloat(yValue, -250);
        }
        valueAnimator.setDuration(300);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(v -> {
            yValue= (float) v.getAnimatedValue();
            connectText.setTranslationY((float) v.getAnimatedValue());
        });
        valueAnimator.start();

        /*TranslateAnimation animation=new TranslateAnimation(0,0,-200,0);
        animation.setDuration(2000);
        connectText.startAnimation(animation);*/
    }

    private void setCircleLinener() {
        circleOpenView.setOnChangeListener(new CircleOpenView.ChangeListener() {
            @Override
            public void onChangeListener(int value) {
                // 将布局添加进去
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (value == 2) {
                    transaction.hide(messageFragment);
                    transaction.show(friendFragment);

                } else if (value == 0) {
                    transaction.show(messageFragment);
                    transaction.hide(friendFragment);
                }
                transaction.commit();
            }

            @Override
            public void onChanged(int value) {
                if (value==0){
                    connectText.setText("消息");
                }else if (value==1){
                    connectText.setText("取消");
                }else {
                    connectText.setText("联系人");
                }
            }

            @Override
            public void onTouchDown(boolean isTouchDown) {
                testAnim(isTouchDown);
            }
        });
    }

    private void setConnectText() {

        connectText.setTranslationY(-250);
        connectText.setText("消息");
        connectText.setBackgroundColor(0xacffffff);
    }

    private void init() {
        messageFragment = new MessageFragment(context);
        friendFragment = new FriendFragment(context);

        // 将布局添加进去
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment, messageFragment);
        transaction.add(R.id.fragment, friendFragment);
        transaction.hide(friendFragment);
        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            //leftView.close();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}